package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.Salary;

import com.example.entity.Teacher;
import com.example.service.SalaryService;
import org.apache.ibatis.annotations.Mapper;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("salarys")
public class SalaryController {

    @Autowired
    private SalaryService salaryService;

    /**
     * 薪水分页查询all
     * @return
     */
    @GetMapping("/getPageSalary/{current}/{size}")
    public R getPageSalary(@PathVariable int current,@PathVariable int size){

        IPage page =new Page(current,size);

        IPage page1 = salaryService.page(page);

        return R.success("薪水分页查询！",page1);
    }

    /**
     * 添加薪水
     * @return
     */
    @PostMapping("/addSalary")
    public R addSalary(@RequestBody Salary salary){
        boolean save = salaryService.save(salary);
        if (save){
            return R.success("添加薪水成功！",null);
        }else {
            return R.error("添加薪水失败！");
        }
    }

    /**
     * 通过id查询薪水
     * @return
     */
    @GetMapping("/getSalaryById")
    public R getSalaryById(@PathVariable int id){

        LambdaQueryWrapper<Salary> lqw =new LambdaQueryWrapper<>();
        lqw.eq(Salary::getId,id);
        Salary one = salaryService.getOne(lqw);
        return R.success("通过id查询薪水",one);
    }

    /**
     * 通过id修改薪水
     * @return
     */
    @PutMapping("/updateSalary")
    public R updateSalary(@RequestBody Salary salary){
        boolean b = salaryService.updateById(salary);
        if (b){
            return R.success("通过id修改薪水成功",salary);
        }else {
            return R.error("通过id修改薪水失败！");
        }
    }

    /**
     * 通过id删除薪水
     * @return
     */
    @DeleteMapping("/deleteSalary/{id}")
    public R deleteSalary(@PathVariable int id){

        boolean b = salaryService.removeById(id);
        if (b){
            return R.success("通过id删除薪水成功",id);
        }else {
            return R.error("通过id删除薪水失败！");
        }
    }
    /**
     * 通过工号id和类型查找当前个人薪水
     * @return
     */
    @GetMapping("/getPageSalaryByJobNumber/{current}/{size}/{jobNumber}")
    public R getPageSalaryByJobNumber(@PathVariable int current,@PathVariable int size,@PathVariable int jobNumber,String code){


        LambdaQueryWrapper<Salary> lqw =new LambdaQueryWrapper<>();
        lqw.eq(Salary::getJobNumber,jobNumber);
        lqw.eq(Salary::getType,code);


        IPage page =new Page(current,size);

        IPage page1 = salaryService.page(page, lqw);

        return R.success("通过工号id和类型查找当前个人薪水",page1);
    }
}

